package com.fastwire.carriere.rad.entity;

import java.util.Date;
import java.util.Set;

import com.fastwire.carriere.common.constant.AppConstants;
import com.fastwire.carriere.rad.util.NetworkUtil;

/*
 * Creted on 20120225
 * @author: Lei Jun
 */
// RAD IpMuxL2
public class Rad
{
    private long oid;

    private int version;

    private String ipAddr = null;

    private String swVersion;  // software version

    private String sysDescr;

    private String sysObjectID;

    // The time (in hundredths of a second) since the network management portion 
    // of the system was last re-initialized
    private String sysUpTime;

    private String sysContact;

    private String sysName;

    private String sysLocation;

    private String sysServices;

    private String defaultGWValidate;

    private Date discoveryDate;

    private String status;

    private Date invalidateDate;

    private String neID; // neID ?? the NE which connects to the RAD
    
    private Set<RadTermPoint> termPoints;

    public Rad()
    {

    }

    public Rad(String ip)
    {
        this.ipAddr = ip;
    }

    /**
     * @return the oid
     */
    public long getOid()
    {
        return oid;
    }

    /**
     * @param oid the oid to set
     */
    public void setOid(long oid)
    {
        this.oid = oid;
    }

    /**
     * @return the version
     */
    public int getVersion()
    {
        return version;
    }

    /**
     * @param version the version to set
     */
    public void setVersion(int version)
    {
        this.version = version;
    }

    /**
     * @return the invalidateDate
     */
    public Date getInvalidateDate()
    {
        return invalidateDate;
    }

    /**
     * @param invalidateDate the invalidateDate to set
     */
    public void setInvalidateDate(Date invalidateDate)
    {
        this.invalidateDate = invalidateDate;
    }

    /**
     * @return the ipAddr
     */
    public String getIpAddr()
    {
        return ipAddr;
    }

    /**
     * @param ipAddr the ipAddr to set
     */
    public void setIpAddr(String ipAddr)
    {
        this.ipAddr = ipAddr;
    }

    /**
     * @return the swVersion
     */
    public String getSwVersion()
    {
        return swVersion;
    }

    /**
     * @param swVersion the swVersion to set
     */
    public void setSwVersion(String swVersion)
    {
        this.swVersion = swVersion;
    }

    /**
     * @return the sysDescr
     */
    public String getSysDescr()
    {
        return sysDescr;
    }

    /**
     * @param sysDescr the sysDescr to set
     */
    public void setSysDescr(String sysDescr)
    { 
        this.sysDescr = sysDescr;
    }

    /**
     * @return the sysObjectID
     */
    public String getSysObjectID()
    {
        return sysObjectID;
    }

    /**
     * @param sysObjectID the sysObjectID to set
     */
    public void setSysObjectID(String sysObjectID)
    {
        this.sysObjectID = sysObjectID;
    }

    /**
     * @return the sysUpTime
     */
    public String getSysUpTime()
    {
        return sysUpTime;
    }

    /**
     * @param sysUpTime the sysUpTime to set
     */
    public void setSysUpTime(String sysUpTime)
    {
        this.sysUpTime = sysUpTime;
    }

    /**
     * @return the sysContact
     */
    public String getSysContact()
    {
        return sysContact;
    }

    /**
     * @param sysContact the sysContact to set
     */
    public void setSysContact(String sysContact)
    {
        this.sysContact = sysContact;
    }

    /**
     * @return the sysName
     */
    public String getSysName()
    {
        return sysName;
    }

    /**
     * @param sysName the sysName to set
     */
    public void setSysName(String sysName)
    {
        this.sysName = sysName;
    }

    /**
     * @return the sysLocation
     */
    public String getSysLocation()
    {
        return sysLocation;
    }

    /**
     * @param sysLocation the sysLocation to set
     */
    public void setSysLocation(String sysLocation)
    {
        this.sysLocation = sysLocation;
    }

    /**
     * @return the sysServices
     */
    public String getSysServices()
    {
        return sysServices;
    }

    /**
     * @param sysServices the sysServices to set
     */
    public void setSysServices(String sysServices)
    {
        this.sysServices = sysServices;
    }

    /**
     * @return the defaultGWValidate
     */
    public String getDefaultGWValidate()
    {
        return defaultGWValidate;
    }

    /**
     * @param defaultGWValidate the defaultGWValidate to set
     */
    public void setDefaultGWValidate(String defaultGWValidate)
    {
        this.defaultGWValidate = defaultGWValidate;
    }

    // by now, only support IPv4
    public void setDefaultGWValidate(String defGW, String hostIp, String hostIpMask)
    {
        String gwValid = null;

        if (!NetworkUtil.ipv4Check(defGW))
        {
            gwValid = "default GW is not correct: " + defGW;
            System.out.println(gwValid);
        }
        else if (!NetworkUtil.ipv4Check(hostIp))
        {
            gwValid = "host IP is not correct: " + hostIp;
            System.out.println(gwValid);
        }
        else if (!NetworkUtil.ipv4Check(hostIpMask))
        {
            gwValid = "host IP Mask is not correct: " + hostIpMask;
            System.out.println(gwValid);
        }
        if (null != gwValid)
        {
            setDefaultGWValidate(gwValid);
            return;
        }

        if (!"0.0.0.0".equalsIgnoreCase(defGW) || !defGW.isEmpty())
        {
            boolean defGWValid = true;

            String[] gwAry = defGW.split("\\.");
            String[] ipAry = hostIp.split("\\.");
            String[] maskAry = hostIpMask.split("\\.");

            for (int i = 0; i < gwAry.length; i++)
            {
                int gwp = Integer.parseInt(gwAry[i]);
                int ipp = Integer.parseInt(ipAry[i]);
                int maskp = Integer.parseInt(maskAry[i]);

                if ((ipp & maskp) != (gwp & maskp))
                {
                    defGWValid = false;
                    break;
                }
            }

            if (defGWValid)
            {
                gwValid = "OK.";
            }
            else
            {
                gwValid = "Illegal Default Gateway address.\n Host IP Address and Default Gateway must be from the same sub-net.";
            }
        }
        else
        {
            gwValid = "Not checked.";
        }

        this.defaultGWValidate = gwValid;
    }

    /**
     * @return the discoveryDate
     */
    public Date getDiscoveryDate()
    {
        return discoveryDate;
    }

    /**
     * @param discoveryDate the discoveryDate to set
     */
    public void setDiscoveryDate(Date discoveryDate)
    {
        this.discoveryDate = discoveryDate;
    }

    /**
     * @return the status
     */
    public String getStatus()
    {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status)
    {
        this.status = status;
    }

    public void invalidate()
    {
        for(RadTermPoint tp : termPoints)
        {
            tp.invalidate();
        }
        
        setInvalidateDate(new Date());        
        setStatus(AppConstants.NE_STATUS_INACTIVE);
    }

    public void validate()
    {
        // setEndDate(null);
        setStatus(AppConstants.NE_STATUS_ACTIVE);
    }

    /**
     * @return the neID
     */
    public String getNeID()
    {
        return neID;
    }

    /**
     * @param neID the neID to set
     */
    public void setNeID(String neID)
    {
        this.neID = neID;
    }

    /**
     * @return the termPoints
     */
    public Set<RadTermPoint> getTermPoints()
    {
        return termPoints;
    }

    /**
     * @param termPoints the termPoints to set
     */
    public void setTermPoints(Set<RadTermPoint> termPoints)
    {
        this.termPoints = termPoints;
    }

}
